What is cloudevents?
The 'cloudevents' npm package is a library for working with CloudEvents, which are a specification for describing event data in a common way. This package allows you to create, validate, and serialize/deserialize CloudEvents, making it easier to work with event-driven architectures.
What are cloudevents's main functionalities?
Create CloudEvent
This feature allows you to create a new CloudEvent instance with specified attributes such as type, source, and data.
const { CloudEvent } = require('cloudevents');
const myEvent = new CloudEvent({
type: 'com.example.someevent',
source: '/mycontext',
data: {
foo: 'bar'
}
});
console.log(myEvent);
Validate CloudEvent
This feature allows you to validate a CloudEvent instance to ensure it conforms to the CloudEvents specification.
const { CloudEvent, ValidationError } = require('cloudevents');
const myEvent = new CloudEvent({
type: 'com.example.someevent',
source: '/mycontext',
data: {
foo: 'bar'
}
});
try {
myEvent.validate();
console.log('Event is valid');
} catch (e) {
if (e instanceof ValidationError) {
console.error('Event is invalid:', e.errors);
}
}
Serialize CloudEvent
This feature allows you to serialize a CloudEvent instance to a JSON string, which can be useful for transmitting the event over a network.
const { CloudEvent } = require('cloudevents');
const myEvent = new CloudEvent({
type: 'com.example.someevent',
source: '/mycontext',
data: {
foo: 'bar'
}
});
const serializedEvent = myEvent.toString();
console.log(serializedEvent);
Deserialize CloudEvent
This feature allows you to deserialize a JSON string back into a CloudEvent instance, making it easier to work with received events.
const { CloudEvent } = require('cloudevents');
const serializedEvent = '{"type":"com.example.someevent","source":"/mycontext","data":{"foo":"bar"}}';
const myEvent = CloudEvent.parse(serializedEvent);
console.log(myEvent);
Other packages similar to cloudevents
eventemitter3
EventEmitter3 is a high-performance event emitter for Node.js and the browser. While it does not specifically adhere to the CloudEvents specification, it provides a robust way to handle events in an application.
node-eventstore-client
Node EventStore Client is a client for EventStore, a database optimized for event sourcing. It provides more advanced features for event storage and retrieval but is more complex and specific to EventStore.
aws-sdk
The AWS SDK for JavaScript provides a way to interact with AWS services, including event-driven services like AWS Lambda and Amazon EventBridge. It offers broader functionality but is specific to the AWS ecosystem.
JavaScript SDK for CloudEvents
The CloudEvents SDK for JavaScript.
Features
- Represent CloudEvents in memory
- Serialize and deserialize CloudEvents in different event formats.
- Send and recieve CloudEvents with via different protocol bindings.
Note: Supports CloudEvent versions 0.3, 1.0
Installation
The CloudEvents SDK requires a current LTS version of Node.js. At the moment
those are Node.js 10.x and Node.js 12.x. To install in your Node.js project:
npm install cloudevents
Receiving and Emitting Events
Receiving Events
You can choose any popular web framework for port binding. A CloudEvent
object can be created by simply providing the HTTP
protocol binding
the incoming headers and request body.
const app = require("express")();
const { HTTP } = require("cloudevents");
app.post("/", (req, res) => {
const receivedEvent = HTTP.toEvent({ headers: req.headers, body: req.body });
console.log(receivedEvent);
});
Emitting Events
You can send events over HTTP in either binary or structured format
using the HTTP
binding to create a Message
which has properties
for headers
and body
.
const axios = require("axios").default;
const { HTTP, CloudEvent } = require("cloudevents");
const ce = new CloudEvent({ type, source, data });
const message = HTTP.binary(ce);
axios({
method: "post",
url: "...",
data: message.body,
headers: message.headers,
});
You may also use the emitterFor()
function as a convenience.
const axios = require("axios").default;
const { emitterFor, Mode, CloudEvent } = require("cloudevents");
function sendWithAxios(message) {
axios({
method: "post",
url: "...",
data: message.body,
headers: message.headers,
});
}
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
emit(new CloudEvent({ type, source, data }));
You may also use the Emitter
singleton
const axios = require("axios").default;
const { emitterFor, Mode, CloudEvent, Emitter } = require("cloudevents");
function sendWithAxios(message) {
axios({
method: "post",
url: "...",
data: message.body,
headers: message.headers,
});
}
const emit = emitterFor(sendWithAxios, { mode: Mode.BINARY });
Emitter.on("cloudevent", emit);
...
new CloudEvent({ type, source, data }).emit();
CloudEvent Objects
All created CloudEvent
objects are read-only. If you need to update a property or add a new extension to an existing cloud event object, you can use the cloneWith
method. This will return a new CloudEvent
with any update or new properties. For example:
const {
CloudEvent,
} = require("cloudevents");
const ce = new CloudEvent({...});
const ce2 = ce.cloneWith({extension: "Value"});
Example Applications
There are a few trivial example applications in
the examples folder.
There you will find Express.js, TypeScript and Websocket examples.
API Transition Guide
Guide Link
Supported specification features
Core Specification | v0.3 | v1.0 |
---|
CloudEvents Core | :heavy_check_mark: | :heavy_check_mark: |
Event Formats | v0.3 | v1.0 |
---|
AVRO Event Format | :x: | :x: |
JSON Event Format | :heavy_check_mark: | :heavy_check_mark: |
Transport Protocols | v0.3 | v1.0 |
---|
AMQP Protocol Binding | :x: | :x: |
HTTP Protocol Binding | :heavy_check_mark: | :heavy_check_mark: |
Kafka Protocol Binding | :x: | :x: |
MQTT Protocol Binding | :x: | :x: |
NATS Protocol Binding | :x: | :x: |
Contributing
We love contributions from the community! Please check the
Contributor's Guide
for information on how to get involved.
Each SDK may have its own unique processes, tooling and guidelines, common
governance related material can be found in the
CloudEvents community
directory. In particular, in there you will find information concerning
how SDK projects are
managed,
guidelines
for how PR reviews and approval, and our
Code of Conduct
information.